function validasi_nama(nama)
{
	var expnama = /^[a-z|A-Z|\s]{5,}$/g;
	var validasi_nama, color;
	if (nama.length==0)
	{
		validasi_nama="nama belum dimasukkan";
		color = "red";
	}
	else
		if (nama.match(expnama))
		{
			validasi_nama = "<br/>";
			color = "green";
		}
		else
		{
			validasi_nama = "nama hanya bisa berisi huruf dan spasi, minimal 5 karakter";
			color = "red";
		}
	document.getElementById("validasi_nama").innerHTML = '<font color="'+ color+'">'+validasi_nama+'</font>';
        return color == "green";
}

function validasi_name(name)
{
	var expname = /^[a-z|A-Z|0-9|.|_]{5,}$/g;
	var validasi_name, color;
	if (name.length==0)
	{
		validasi_name="username belum dimasukkan";
		color = "red";
	}
	else
		if (name.match(expname))
		{
			validasi_name = "<br/>";
			color = "green";
		}
		else
		{
			validasi_name = "username hanya bisa berisi huruf angka, titik, dan underscore, minimal 5 karakter";
			color = "red";
		}
	document.getElementById("validasi_name").innerHTML = '<font color="'+ color+'">'+validasi_name+'</font>';
        return color == "green";
}

function validasi_pass(password)
{
	var exppass = /^.{6,}$/g;
	var validasi_pass, color;
	if (password.length==0)
	{
		validasi_pass="password belum dimasukkan";
		color = "red";
	}
	else
		if (password.match(exppass))
		{
			validasi_pass = "<br/>";
			color = "green";
		}
		else
		{
			validasi_pass = "password minimal 6 karakter";
			color = "red";
		}
	document.getElementById("validasi_pass").innerHTML = '<font color="'+ color+'">'+validasi_pass+'</font>';
        return color == "green";
}

function beda(s1, s2)
{
	var i;
	if (s1.length!=s2.length)
		return true;
	else
	{
		for (i=0; (i<s1.length)&&(s1.charAt(i)==s2.charAt(i)); ++i);
		return (i<s1.length);
	}
}

function is_tanggal(hari, bulan, tahun)
{
	var HARI = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if ((tahun < 0) || (bulan <=0) || (bulan>12) || (hari <= 0))
		return false;
	if ((tahun % 400 == 0)||(tahun % 100 != 0 && tahun % 4 == 0))
		++HARI[1];
	return (hari <= HARI[bulan-1]);
}

function validasi_tgl(tanggal)
{
	var exptgl = /^\d{4}-\d{2}-\d{2}$/g;
	var validasi_tgl, color;
	var y,m,d;
	if (tanggal.length==0)
	{
		validasi_tgl="tanggal belum dimasukkan";
		color = "red";
	}
	else
		if (tanggal.match(exptgl))
		{
			y = (Number(tanggal.charAt(0)))*1000+(Number(tanggal.charAt(1)))*100+(Number(tanggal.charAt(2)))*10+Number(tanggal.charAt(3));
			m = (Number(tanggal.charAt(5)))*10+Number(tanggal.charAt(6));
			d = (Number(tanggal.charAt(8)))*10+Number(tanggal.charAt(9));
			if (is_tanggal(d, m, y))
			{
				validasi_tgl = "<br/>";
				color = "green";
			}
			else
			{
				validasi_tgl = "tanggal tidak valid";
				color = "red";
			}
		}
		else
		{
			validasi_tgl = "gunakan format YYYY-MM-DD";
			color = "red";
		}
	document.getElementById("validasi_tgl").innerHTML = '<font color="'+ color+'">'+validasi_tgl+'</font>';
        return color == "green";
}

function validasi_email(email)
{
        var expemail = /([a-z]|[0-9]|_|\.)+@.+\.[a-z]{1,}$/gi;
	var validasi_email, color;
	if (email.length==0)
	{
		validasi_email="email belum dimasukkan";
		color = "red";
	}
	else
		if (email.match(expemail))
		{
			validasi_email = "<br/>";
			color = "green";
		}
		else
		{
			validasi_email = "email tidak valid";
			color = "red";
		}
	document.getElementById("validasi_email").innerHTML = '<font color="'+ color+'">'+validasi_email+'</font>';
        return color == "green";
}

function hehehe()
{
	if (document.getElementById("submit").disabled)
		validasi();
}

function validasi()
{
    var valid = true;
	var pass1 = document.getElementById("password").value;
	var pass2 = document.getElementById("passwordAgain").value;
	valid = validasi_nama(document.getElementById("nama").value) && valid;
	//valid = validasi_name(document.getElementById("username").value) && valid;
	//valid = validasi_pass(pass1) && valid;
	if (beda(pass1,pass2)){
		document.getElementById("validasi_pass2").innerHTML = '<font color="red">masukan password berbeda</font>';
                valid = valid && false;
        }else{
		document.getElementById("validasi_pass2").innerHTML = '<br/>';
                valid = valid && true;
        }
	valid = validasi_tgl(document.getElementById("tanggalLahir").value)  && valid;
	valid = validasi_email(document.getElementById("email").value)  && valid;

	if (!valid){
		document.getElementById("submit").disabled = "disabled";
        }
	else{
		document.getElementById("submit").disabled = false;
        }
}


function validasi_foto(foto)
{
	var expfoto = /^[^\/:*?<>]+(.)[bmp|jpg|jpeg|png]$/g;
	var validasi_foto, color;
	if (foto.length==0)
	{
		return false;
	}
	else if(foto.match(expfoto))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function uploadfoto(){
	/*var path = $('filename').value;
	var ambil = /\\.*$/gi;
	alert(ambil.exec(path));*/
	var path=$('filename').value.substr($('filename').value.lastIndexOf('\\') + 1);
	if (validasi_foto(path)){
		document.forms['upload'].submit();
	}
	else{
		alert('File name is empty or invalid.');		
	}
}